﻿/*
 * Cratos Generator
 * Cratos Application Startup class
 * 
 * Copyright (c) 2011 Draco
 * 
 * This file is part of Cratos.
 * 
 * Cratos is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Cratos is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cratos.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *       .==.        .==.
 *      //'^\\      //^'\\
 *     // ^ ^\(\__/)/^ ^^\\
 *    //^ ^^ ^/6  6\ ^^ ^ \\
 *   //^ ^^ ^/( .. )\^ ^ ^ \\
 *  // ^^ ^/\| V""V|/\^ ^ ^ \\
 * //^^/\/ /  '~~'  \ \/\^ ^ \\
 *  ------------------------------------
 *  HERE BE DRAGONS
 *  
 *  Johan Dercksen - johan@draco.co.za
 */
using System;
using System.Data;
using System.Linq;
using System.Windows;
using System.Configuration;
using System.Collections.Generic;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

using Cratos.Desktop;
using Cratos.Tools.DataObject;
using Cratos.Tools.General;

namespace Cratos.Generator
{
	/// <summary>
	/// Application Startup class
	/// </summary>
	public partial class ApplicationStartup : Application
	{
		#region Class Constant declarations
		#endregion

		#region Class Event declarations
		#endregion

		#region Class Variable declarations
		private ApplicationShell m_ApplicationShell;
		#endregion

		#region Class Constructors & Destructors
		/// <summary>
		/// Class Constructor
		/// </summary>
		public ApplicationStartup()
		{
			// Initialize the relevant components
			InitializeComponent();
		}
		#endregion

		#region Class Properties
		/// <summary>
		/// Startup Completed indicator
		/// </summary>
		public bool IsStartupCompleted { get; set; }

		/// <summary>
		/// Application Unity Container
		/// </summary>
		public static IUnityContainer UnityContainer { get; set; }
		#endregion

		#region Class Methods
		private void OnApplicationStartup(object sender, StartupEventArgs e)
		{
			// Declare the required variables
			SplashWindow splashWindow;

			// Make sure we only perform the startup once
			if (this.IsStartupCompleted) { return; }

			try
			{
				#region Main Window
				// Create and setup the Main Window
				this.m_ApplicationShell        = new ApplicationShell();
				Application.Current.MainWindow = this.m_ApplicationShell;
				#endregion

				#region Splash Screen setup
				// Create and setup the required objects
				splashWindow = new SplashWindow();
				splashWindow.AddStep("Initializing the application");
				splashWindow.AddStep("Setup the application environment");
				splashWindow.AddStep("Setup the server connection");
				splashWindow.AddStep("Initialization completed");
				splashWindow.Step(false);
				splashWindow.Show();
				#endregion

				#region Application Environment Setup
				// Step the task bar on the splash screen
				splashWindow.Step(true);

				// Create and Setup the Unity Container
				ApplicationStartup.UnityContainer = new UnityContainer().LoadConfiguration();
				#endregion

				#region Server Setup
				// Step the task bar on the splash screen
				splashWindow.Step(true);

				// Set the relevant options
				CratosHelper.LogFilename = ConfigurationManager.AppSettings["LogFilename"];

				DOCratosGlobal.UnityContainer               = ApplicationStartup.UnityContainer;
				DOCratosGlobal.EntityAccessMethod           = (CratosEntityAccessType)Enum.Parse(typeof(Cratos.Tools.General.CratosEntityAccessType), ConfigurationManager.AppSettings["CratosEntityAccess"]);
				DOCratosGlobal.EntityCollectionAccessMethod = ConfigurationManager.AppSettings["CratosEntityCollectionAccess"];
				DOCratosGlobal.DBConnectionName             = ConfigurationManager.AppSettings["DBConnectionName"];
				DOCratosGlobal.WcfServiceAddress            = new Uri(ConfigurationManager.AppSettings["CratosServiceAddress"]);
				#endregion

				// Hide the Splash Window
				splashWindow.Close();

				// Show the Main Window
				Application.Current.MainWindow.Show();

				// Set the Startup indicator
				this.IsStartupCompleted = true;
			}
			catch (Exception runtimeException)
			{
				// Display the exception
				CratosHelper.LogMessage(CratosLogSeverity.Exception, "Cratos Generator", 
																"Unhandled exception occurred", runtimeException, true);
			}
			finally
			{
			}

			// Return to calling method
			return;
		}
		#endregion

		#region Class Support Methods
		#endregion
	}
}
